package org.jeecg.modules.system.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.system.entity.Manufacturer;
import org.jeecg.modules.system.entity.ManufacturerStaff;
import org.jeecg.modules.system.mapper.ManufacturerMapper;
import org.jeecg.modules.system.mapper.ManufacturerStaffMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 厂商管理
 * @Author: jeecg-boot
 * @Date: 2024-07-12
 * @Version: V1.0
 */
@Service
public class ManufacturerServiceImpl extends ServiceImpl<ManufacturerMapper, Manufacturer> implements IManufacturerService {

    @Resource
    private ManufacturerMapper manufacturerMapper;
    @Resource
    private ManufacturerStaffMapper manufacturerStaffMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(Manufacturer manufacturer, List<ManufacturerStaff> manufacturerStaffList) {
        manufacturerMapper.insert(manufacturer);
        if (manufacturerStaffList != null && manufacturerStaffList.size() > 0) {
            for (ManufacturerStaff entity : manufacturerStaffList) {
                //外键设置
                entity.setManufacturerId(manufacturer.getId());
                manufacturerStaffMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(Manufacturer manufacturer, List<ManufacturerStaff> manufacturerStaffList) {
        manufacturerMapper.updateById(manufacturer);

        //1.先删除子表数据
        manufacturerStaffMapper.deleteByMainId(manufacturer.getId());

        //2.子表数据重新插入
        if (manufacturerStaffList != null && manufacturerStaffList.size() > 0) {
            for (ManufacturerStaff entity : manufacturerStaffList) {
                //外键设置
                entity.setManufacturerId(manufacturer.getId());
                manufacturerStaffMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        manufacturerStaffMapper.deleteByMainId(id);
        manufacturerMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            manufacturerStaffMapper.deleteByMainId(id.toString());
            manufacturerMapper.deleteById(id);
        }
    }

}
